package com.zh.common.view

import android.annotation.SuppressLint
import android.content.Context
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import com.blankj.utilcode.util.ScreenUtils
import com.zh.common.ktx.dp2px
import com.zh.common.ktx.statusBarHeight
import kotlin.math.abs

/**
 * 可拖动浮框
 */
@SuppressLint("ClickableViewAccessibility")
class FloatingView @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null
) : FrameLayout(context, attrs) {

    //初始数据
    private var marginHorizontal = 20f.dp2px
    private var topMargin = 0
    private var bottomMargin = 0
    private var isAutoScreen = false
    private var isAutoLeftScreen = false
    private var isAutoRightScreen = false
    private var isDrag = true

    private var iView: View? = null
    var onClickListener: (() -> Unit)? = null

    /**
     * 是否可以拖动
     */
    fun isDrag(isDrag: Boolean): FloatingView {
        this.isDrag = isDrag
        return this
    }

    /**
     * 距离两边距离
     */
    fun setMarginHorizontal(margin: Int): FloatingView {
        marginHorizontal = margin
        return this
    }

    /**
     * 距离顶部距离
     */
    fun setTopMargin(margin: Int): FloatingView {
        topMargin = margin
        return this
    }

    /**
     * 是否自定靠边
     */
    fun setIsAutoScreen(bool: Boolean): FloatingView {
        isAutoScreen = bool
        return this
    }

    /**
     * 自定靠左边
     */
    fun setIsAutoLeftScreen(bool: Boolean): FloatingView {
        isAutoLeftScreen = bool
        return this
    }

    /**
     * 自动靠右边
     */
    fun setIsAutoRightScreen(bool: Boolean): FloatingView {
        isAutoRightScreen = bool
        return this
    }

    /**
     * 距离底部距离
     */
    fun setBottomMargin(margin: Int): FloatingView {
        bottomMargin = margin
        return this
    }

    /**
     * 添加浮框内容View
     */
    fun setContentView(view: View): FloatingView {
        iView = view
        addView(iView, LayoutParams(LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT))
        return this
    }

    private var dx = 0f
    private var dy = 0f
    private var sx = 0f
    private var sy = 0f
    private var isMove = false

    override fun onTouchEvent(event: MotionEvent): Boolean {
        when (event.action) {
            MotionEvent.ACTION_DOWN -> {
                if (isDrag) {
                    dx = event.rawX
                    dy = event.rawY
                    sx = event.x
                    sy = event.y
                    isMove = true
                }
            }

            MotionEvent.ACTION_MOVE -> {
                if (isDrag) {
                    val mx = event.rawX - dx
                    val my = event.rawY - dy

                    val ml = ScreenUtils.getScreenWidth() - width - marginHorizontal
                    val left = if (this.left + mx <= marginHorizontal) {
                        marginHorizontal
                    } else if (this.left + mx > ml) {
                        ml
                    } else {
                        abs(this.left + mx)
                    }

                    val mt =
                        ScreenUtils.getAppScreenHeight() - height - bottomMargin - statusBarHeight
                    val top = if (this.top + my < topMargin) {
                        topMargin
                    } else if (this.top + my > mt) {
                        mt
                    } else {
                        abs(this.top + my)
                    }
                    this.layoutParams = (this.layoutParams as LayoutParams).apply {
                        topMargin = top.toInt()
                        leftMargin = left.toInt()
                    }
                    dx = event.rawX
                    dy = event.rawY
                    if (abs(sx - event.x) >= 5 || abs(sy - event.y) >= 5) {//执行onclick事件
                        isMove = false
                    }
                }
            }

            MotionEvent.ACTION_UP -> {
                if (isDrag) {
                    if (isAutoScreen) {//靠边
                        val mx = event.rawX - dx
                        val ml = ScreenUtils.getScreenWidth() - width - marginHorizontal
                        val left =
                            if (this.left + mx + width / 2 > ScreenUtils.getScreenWidth() / 2) {
                                ml
                            } else {
                                marginHorizontal
                            }
                        this.layoutParams = (this.layoutParams as LayoutParams).apply {
                            leftMargin = left
                        }
                    } else if (isAutoLeftScreen) {//靠左
                        this.layoutParams = (this.layoutParams as LayoutParams).apply {
                            leftMargin = marginHorizontal
                        }
                    } else if (isAutoRightScreen) {//靠右
                        this.layoutParams = (this.layoutParams as LayoutParams).apply {
                            leftMargin = ScreenUtils.getScreenWidth() - width - marginHorizontal
                        }
                    }

                    if (isMove) {//执行onclick事件
                        onClickListener?.invoke()
                    }
                    dx = event.rawX
                    dy = event.rawY
                }
            }
        }
        return true
    }
}